package com.briup.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.server.domain.bean.RoleMenu;
import com.briup.server.service.RoleMenuService;
import com.briup.server.mapper.RoleMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
        implements RoleMenuService {

    @Transactional
    @Override
    public void grantPermissions(Long roleId, List<Integer> menuIds) {
        // 先在桥表中删除角色对应的所有权限
        remove(Wrappers.<RoleMenu>lambdaQuery()
                .eq(RoleMenu::getRoleId, roleId));
        // 在将数据重新添加回桥表
        List<RoleMenu> list = menuIds.stream()
                .map(menuId -> {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(Long.valueOf(menuId));
                    return roleMenu;
                }).toList();
        // 批量添加
        saveBatch(list);
    }
}




